package pers.amos.eshop.advance.product.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import pers.amos.eshop.advance.product.common.DataTypeConstants;
import pers.amos.eshop.advance.product.common.EventTypeConstants;
import pers.amos.eshop.advance.product.mapper.BrandMapper;
import pers.amos.eshop.advance.product.model.Brand;
import pers.amos.eshop.advance.product.mq.RabbitMQSender;
import pers.amos.eshop.advance.product.service.BrandService;

import java.util.List;


@Service
public class BrandServiceImpl implements BrandService {

    @Autowired
    private BrandMapper brandMapper;

    @Autowired
    private RabbitMQSender rabbitMqSender;

    @Override
    public void add(Brand brand, String operationType) {
        brandMapper.add(brand);

        String sendMessage = RabbitMQSender.buildSendData(EventTypeConstants.ADD_EVENT_TYPE,
                DataTypeConstants.BRAND_DATA_TYPE, brand.getId());

        String queue = rabbitMqSender.decideSendMegQueue(operationType);

        rabbitMqSender.send(queue, sendMessage);
    }

    @Override
    public void update(Brand brand, String operationType) {
        brandMapper.update(brand);

        String sendMessage = RabbitMQSender.buildSendData(EventTypeConstants.UPDATE_EVENT_TYPE,
                DataTypeConstants.BRAND_DATA_TYPE, brand.getId());

        String queue = rabbitMqSender.decideSendMegQueue(operationType);

        rabbitMqSender.send(queue, sendMessage);
    }

    @Override
    public void delete(Long id, String operationType) {
        brandMapper.delete(id);

        String sendMessage = RabbitMQSender.buildSendData(EventTypeConstants.DELETE_EVENT_TYPE,
                DataTypeConstants.BRAND_DATA_TYPE, id);

        String queue = rabbitMqSender.decideSendMegQueue(operationType);

        rabbitMqSender.send(queue, sendMessage);
    }

    @Override
    public Brand findById(Long id) {
        return brandMapper.findById(id);
    }

    @Override
    public List<Brand> findBrandListByIds(String ids) {
        return brandMapper.findBrandListByIds(ids);
    }

}
